package cl.parser.internal;

import java.util.ArrayList;
import java.util.List;

import cl.parser.IParseNode;
import cl.parser.external.EqualSignNode;
import cl.parser.external.IdentifierNode;
import cl.parser.external.SemicolonNode;
import cl.parser.external.TypeNode;

public class VariableDeclarationNode extends InternalNode implements IFat {

    @Override
    public String prettyPrint(String indent) {
        String res = children.get(0).prettyPrint(indent) + " " + children.get(1).prettyPrint(indent);
        
        if(children.size() > 2) {
            res += " = " + children.get(2).prettyPrint(indent);
        }
        
        return res + ";\n";
    }
    
    @Override
    protected List<IParseNode> getAllowed(IParseNode last, List<IParseNode> soFar) {
        List<IParseNode> allowed = new ArrayList<IParseNode>();
        
        if(last == null) {
            allowed.add(new TypeNode());
        } else if (last instanceof TypeNode) {
            allowed.add(new IdentifierNode());
        } else if (last instanceof IdentifierNode) {
            allowed.add(new SemicolonNode());
            allowed.add(new EqualSignNode());
        } else if(last instanceof EqualSignNode) {
            allowed.add(new ExpressionNode());
        } else if(last instanceof ExpressionNode) {
            allowed.add(new SemicolonNode());
        }
        
        return allowed;
    }

    @Override
    public void loseWeight() {
        // remove semicolon
        children.remove(children.size() - 1);
        
        if(children.size() > 3) {
            // = sign
            children.remove(children.size() - 2);
        }
    }

    @Override
    protected boolean isValid(IParseNode last, List<IParseNode> soFar) {
        return last instanceof SemicolonNode;
    }

}
